package timlcy.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.thoughtworks.xstream.XStream;

import timlcy.po.Image;
import timlcy.po.ImageMessage;
import timlcy.po.Message;
import timlcy.po.Music;
import timlcy.po.MusicMessage;
import timlcy.po.News;
import timlcy.po.NewsMessage;

public class MessageUtil {
	public static final String MESSAGE_TEXT = "text";
	public static final String MESSAGE_IMAGE = "image";
	public static final String MESSAGE_VOICE = "voice";
	public static final String MESSAGE_VIDEO = "video";
	public static final String MESSAGE_LINK = "link";
	public static final String MESSAGE_LOCATION = "location";
	public static final String MESSAGE_EVENT = "event";
	public static final String MESSAGE_SUBSCRIBE = "subscribe";
	public static final String MESSAGE_UNSUBSCRIBE = "unsubscribe";
	public static final String MESSAGE_CLICK = "CLICK";
	public static final String MESSAGE_VIEW = "VIEW";
	public static final String MESSAGE_NEWS = "news";
	public static final String MESSAGE_MUSIC = "music";
	public static final String MESSAGE_SCANCODE= "scancode_push";
	/**
	 * xml转为map集合
	 * 
	 * @param request
	 * @return
	 * @throws IOException
	 * @throws DocumentException
	 */
	public static Map<String, String> xmlToMap(HttpServletRequest request) throws IOException, DocumentException {
		Map<String, String> map = new HashMap<String, String>();
		SAXReader reader = new SAXReader();
		InputStream ins = request.getInputStream();
		Document doc = reader.read(ins);
		Element root = doc.getRootElement();
		List<Element> list = root.elements();
		for (Element e : list) {
			map.put(e.getName(), e.getText());
		}
		ins.close();
		return map;
	}

	/**
	 * 将文本消息对象转化为XML
	 * 
	 * @param ms
	 * @return
	 */
	public static String MessageToXml(Message ms) {
		XStream xStream = new XStream();
		xStream.alias("xml", ms.getClass());
		return xStream.toXML(ms);
	}

	public static String initText(String ToUserName, String FromUserName, String Content) {
		Message message = new Message();
		message.setFromUserName(ToUserName);
		message.setToUserName(FromUserName);
		message.setMsgType(MESSAGE_TEXT);
		message.setCreateTime(new Date().getTime());
		message.setContent(Content);
		return MessageToXml(message);
	}

	/**
	 * 主菜单
	 * 
	 * @return
	 */
	public static String menuText() {
		StringBuffer sb = new StringBuffer();
		sb.append("欢迎你的关注,请按照菜单提示进行操作:\n\n");
		sb.append("1、学习过程介绍\n");
		sb.append("2、书籍介绍\n");
		sb.append("5、翻译功能介绍\n");
		sb.append("回复?调出此菜单");
		return sb.toString();

	}

	public static String firstMenu() {
		StringBuffer sb = new StringBuffer();
		sb.append("JAVA WEB的学习过程");
		return sb.toString();

	}

	public static String secondMenu() {
		StringBuffer sb = new StringBuffer();
		sb.append("书籍推荐和学习过程");
		return sb.toString();

	}
	public static String threeMenu() {
		StringBuffer sb = new StringBuffer();
		sb.append("词组翻译\n");
		sb.append("示例:翻译+待翻译的词组或句子\n");
		sb.append("如:翻译+足球\n");
		return sb.toString();

	}
	/**
	 * 图文消息转为xml
	 * 
	 * @param ns
	 * @return
	 */
	public static String newsToXml(NewsMessage ns) {
		XStream xStream = new XStream();
		xStream.alias("xml", ns.getClass());
		xStream.alias("item", new News().getClass());
		String xml = xStream.toXML(ns);
		return xml;
	}

	/**
	 * 图片消息转为xml
	 * 
	 * @param im
	 * @return
	 */
	public static String imageToXml(ImageMessage im) {
		XStream xStream = new XStream();
		xStream.alias("xml", im.getClass());
		String xml = xStream.toXML(im);
		return xml;
	}

	/**
	 * 音乐消息转为xml
	 * 
	 * @param mu
	 * @return
	 */
	public static String musicToXml(MusicMessage mu) {
		XStream xStream = new XStream();
		xStream.alias("xml", mu.getClass());
		String xml = xStream.toXML(mu);
		return xml;
	}

	/**
	 * 图文消息的组装
	 * 
	 * @param ToUserName
	 * @param FromUserName
	 * @return
	 */
	public static String initNewsMessage(String ToUserName, String FromUserName) {
		{
			String message = null;
			List<News> newsList = new ArrayList<>();
			NewsMessage newsMessage = new NewsMessage();

			News news = new News();
			news.setTitle("java web");
			news.setPicUrl("http://timlcy.tunnel.qydev.com/weixin/images/sports.jpg");
			news.setUrl("http://study.163.com/course/introduction/1003108028.htm#/courseDetail");

			News news2 = new News();
			news2.setTitle("个人图书后台管理");
			news2.setPicUrl("http://timlcy.tunnel.qydev.com/weixin/images/book1.jpg");
			news2.setUrl("http://timlcy.tunnel.qydev.com/bookstore/");

			News news3 = new News();
			news3.setTitle("在线书签");
			news3.setPicUrl("http://timlcy.tunnel.qydev.com/weixin/images/book2.jpg");
			news3.setUrl("http://timlcy.tunnel.qydev.com/BookmarkOnline/");

			newsList.add(news);
			newsList.add(news2);
			newsList.add(news3);
			newsMessage.setFromUserName(ToUserName);
			newsMessage.setToUserName(FromUserName);
			newsMessage.setMsgType(MESSAGE_NEWS);
			newsMessage.setCreateTime(new Date().getTime());
			newsMessage.setArticles(newsList);
			newsMessage.setArticleCount(newsList.size());
			message = newsToXml(newsMessage);
			return message;
		}
	}

	/**
	 * 组装图片消息
	 * 
	 * @param ToUserName
	 * @param FromUserName
	 * @return
	 */
	public static String initImageMessage(String ToUserName, String FromUserName) {
		String message = null;
		Image image = new Image();
		image.setMediaId("gYKYwvxP5JyDpilvdta6N125LswaFq23y2aAQN0n8Q7apFs7O33fWC-2EpzKW9n_");
		ImageMessage imageMessage = new ImageMessage();
		imageMessage.setFromUserName(ToUserName);
		imageMessage.setToUserName(FromUserName);
		imageMessage.setMsgType(MESSAGE_IMAGE);
		imageMessage.setCreateTime(new Date().getTime());
		imageMessage.setImage(image);
		message = imageToXml(imageMessage);
		return message;
	}

	/**
	 * 组装音乐消息
	 * 
	 * @param ToUserName
	 * @param FromUserName
	 * @return
	 */
	public static String initMusicMessage(String ToUserName, String FromUserName) {
		String message = null;
		Music music = new Music();
		music.setThumbMediaId("lBMY_wg7ihI9gLzv_3_RgroKMi5HtFZOS2KGUlr89Egc88Idpkpe3J3E2-6HZ8gR");
		music.setTitle("see you again");
		music.setDescription("速7片尾曲");
		music.setMusicUrl("http://timlcy.tunnel.qydev.com/weixin/resource/See You Again.mp3");
		music.setHQMusicUrl("http://timlcy.tunnel.qydev.com/weixin/resource/See You Again.mp3");
		
		MusicMessage musicMessage = new MusicMessage();
		musicMessage.setFromUserName(ToUserName);
		musicMessage.setToUserName(FromUserName);
		musicMessage.setMsgType(MESSAGE_MUSIC);
		musicMessage.setCreateTime(new Date().getTime());
		musicMessage.setMusic(music);
		message = musicToXml(musicMessage);
		return message;
	}
}
